﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Controller;
using Entity;
namespace Web
{
    public partial class CustomerDetail : System.Web.UI.Page
    {
        Helper helper = new Helper();
        CustomerEntity customerEntity = new CustomerEntity();
        CustomerController customerController = new CustomerController();
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                int customerId = Convert.ToInt32(Request.QueryString["customerId"]);
                customerEntity = customerController.FindCustomerByCustomerId(customerId);
                NameLabel.Text = customerEntity.Name;
                ShortNameLabel.Text = customerEntity.ShortName;
                NumberLabel.Text = customerEntity.Number.ToString();
                CodeLabel.Text = customerEntity.Code;
                AddressLabel.Text = customerEntity.Address;
                PhoneLabel.Text = customerEntity.Phone;
                FaxLabel.Text = customerEntity.Fax;
                EmailLabel.Text = customerEntity.Email;
                ContactLabel.Text = customerEntity.ContactPerson;
                ZipCodeLabel.Text = customerEntity.ZipCode;
                RemarkLabel.Text = customerEntity.Remark;
            }
            catch (Exception)
            {
                Session["FlashMessage"] = "Error: Invalid Customer Id!";
                Session["FlashMessageType"] = "error";
            }
           
            this.PreRender += new EventHandler(UpdateFlashMessage);
        }
        private void UpdateFlashMessage(object sender, EventArgs e)
        {
            helper.ShowFlashMessage(FlashMessageLabel);
        }

        protected void EditButton_Click(object sender, EventArgs e)
        {
            int customerId = Convert.ToInt32(Request.QueryString["customerId"]);
            Response.Redirect("EditCustomer.aspx?customerId=" + customerId);
        }

        protected void DeleteButton_Click(object sender, EventArgs e)
        {
            int customerId = Convert.ToInt32(Request.QueryString["customerId"]);
            bool result = customerController.DeleteCustomerById(customerId);
            if (result == true)
            {
                Session["FlashMessage"] = "Success: Delete Customer Successful!";
                Session["FlashMessageType"] = "success";
                Response.Redirect("CustomerList.aspx");
            }
            else
            {
                Session["FlashMessage"] = "Error: Delete Customer unsuccessful!";
                Session["FlashMessageType"] = "error";
            }

        }
    }
}